import {combineReducers} from 'redux-immutable';
import * as actions from './actions';
import {GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS, GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL} from 'Shared/actions/currentUserContext';
import {partContractInvoiceStatus} from '../Enum';
import Immutable from 'immutable';

const initData = (state, action) => {
    switch(action.type) {
        case actions.GET_INIT_BEGIN:
            return state.merge({
                isFetching: true,
            });
        case actions.GET_INIT_SUCCESS:
            return state.merge({
                isFetching: false,
                isDealerUser: action.data.isDealerUser,
                brands: action.data.brands
            });
        case actions.GET_INIT_FAIL:
            return state.merge({
                isFetching: false,
            });
        case GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS:
            return state.merge({
                permissions: action.data
            });
        case GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL:
            return state.merge({
                permissions: []
            });
        default:
            return state;
    }
};

const list = (state, action) => {
    switch(action.type) {
        case actions.GET_LIST_BEGIN:
            return state.merge({
                isFetching: true,
                message: '',
                total: 0
            });
        case actions.GET_LIST_SUCCESS:
            return state.merge({
                isFetching: false,
                total: action.data.totalElements,
                message: '',
                data: action.data.content
            });
        case actions.GET_LIST_FAIL:
            return state.merge({
                isFetching: false,
                total: 0,
                message: action.message,
                data: []
            });
        default:
            return state;
    }
};
const detail = (state, action) => {
    switch(action.type) {
        case actions.GET_DETAIL_BEGIN:
            return state.merge({
                isFetching: true,
                message: '',
                data: {}
            });
        case actions.GET_DETAIL_SUCCESS:
            return state.merge({
                isFetching: false,
                message: '',
                data: action.data
            });
        case actions.GET_DETAIL_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message,
                data: {}
            });
        default:
            return state;
    }
};

const submitData = (state, action) => {
    switch(action.type) {
        case actions.GET_DETAIL_SUCCESS:
            return state.merge({...action.data});
        case actions.UPDATE_ATTACHMENTS:
            return state.updateIn(['declareAttachments'], obj => obj.push(Immutable.fromJS(action.data)));
        case actions.REMOVE_ATTACHMENTS: {
            const index = state.get('declareAttachments').findIndex(item => item.get('fileId') === action.id);
            if(index === -1)
                return state;
            return state.updateIn(['declareAttachments'], list => list.splice(index, 1));
        }
        case actions.UPDATE_ATTACHMENTS_CLEAR:
            return state.updateIn(['clearanceAttachments'], obj => obj.push(Immutable.fromJS(action.data)));
        case actions.REMOVE_ATTACHMENTS_CLEAR: {
            const index = state.get('clearanceAttachments').findIndex(item => item.get('fileId') === action.id);
            if(index === -1)
                return state;
            return state.updateIn(['clearanceAttachments'], list => list.splice(index, 1));
        }
        default:
            return state;
    }
};

const queryCondition = (state, action) => {
    switch(action.type) {
        case actions.SAVE_QUERY_CONDITION:
            return state.set(action.name, action.value);
        case actions.RESET_QUERY_PANEL:
            return state.merge({
                dealerCode: undefined,
                partSaleOrderCode: undefined,
                code: undefined,
                partInvoiceCode: undefined,
                status: [partContractInvoiceStatus.发票生成],
                invoicedDate: [],
            });
        default:
            return state;
    }
};

const queryBySort = (state, action) => {
    switch(action.type) {
        case actions.GET_LIST_SUCCESS:
            return state.merge(action.condition);
        default:
            return state;
    }
};

const queryDealer = (state, action) => {
    switch(action.type) {
        case actions.SAVE_QUERY_DEALER:
            return state.merge(action.data);
        default:
            return state;
    }
};

const domainData = combineReducers({
    initData,
    list,
    detail,
    submitData
});

const appState = combineReducers({
    queryCondition,
    queryBySort,
    queryDealer
});

import {createNotificationReducer as notification} from 'Shared/utils/serverNotification';

export default combineReducers({
    domainData,
    appState,
    notification
});

